package cn.blunce.weather.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import cn.blunce.weather.base.AppDB;
import cn.blunce.weather.models.CityID;

/**
 * Created by Experiment on 2016/4/25 0025.
 */
public class Utils {

    private static final String CACHE_PHOTOS = "cache_photo";

    /**
     * Does it download all weather ids and city ids?
     *
     * @param context
     * @return
     */
    public static boolean isExistWeatherID_CitiesID(Context context) {
        AppDB appDB = AppDB.getInstance(context);
        return (appDB.getAllCityIDs().size() > 0) && (appDB.getAllWeatherIDs().size() > 0);
    }

    /**
     * Read all cities which are interesting.
     *
     * @param context
     * @param appDB
     * @return
     */
    public static List<CityID> readCities(Context context, AppDB appDB) {
        List<CityID> cities = new ArrayList<>();
        SharedPreferences sharedPreferences = context.getSharedPreferences("city_ids", Context.MODE_PRIVATE);
        int length = sharedPreferences.getInt("length", 0);
        if (length == 0)
            cities.add(appDB.queryCityByCityID("CN101280601").get(0));
        else
            for (int i = 0; i < length; i++)
                cities.add(appDB.queryCityByCityID(sharedPreferences.getString(String.valueOf(i), "")).get(0));
        return cities;
    }

    /**
     * Save all city IDs which are interesting.
     *
     * @param context
     * @param cities
     */
    public static void saveCityIDs(Context context, List<CityID> cities) {
        SharedPreferences sharedPreferences = context.getSharedPreferences("city_ids", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putInt("length", cities.size());
        for (int i = 0; i < cities.size(); i++)
            editor.putString(String.valueOf(i), cities.get(i).getId());
        editor.commit();
    }

    public static List<String> getCityNames(List<CityID> cities) {
        List<String> names = new ArrayList<>();
        for (CityID city : cities)
            names.add(city.getCity());
        return names;
    }


    /**
     * Read weather photo.
     *
     * @param context
     * @param weatherCode
     * @return
     */
    public static Bitmap readPhotoCache(Context context, String weatherCode) {
        try {
            File file = new File(context.getFilesDir(), weatherCode);
            FileInputStream fileInputStream = new FileInputStream(file);
            int length = fileInputStream.available();
            byte[] buffer = new byte[length];
            fileInputStream.read(buffer);
            fileInputStream.close();
            return BitmapFactory.decodeByteArray(buffer, 0, length);
        } catch (FileNotFoundException e) {
//            e.printStackTrace();
            return null;
        } catch (IOException e) {
//            e.printStackTrace();
            return null;
        }
    }


    /**
     * Save weather photo.
     *
     * @param weatherCode
     * @param photo
     * @throws IOException
     */
    public static void savePhotoCache(Context context, String weatherCode, Bitmap photo) {
        try {
            File file = new File(context.getFilesDir(), weatherCode);
            FileOutputStream fileOutputStream = new FileOutputStream(file);
            BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fileOutputStream);
            photo.compress(Bitmap.CompressFormat.PNG, 100, bufferedOutputStream);
            bufferedOutputStream.flush();
            bufferedOutputStream.close();
            fileOutputStream.close();
        } catch (Exception e) {
//            e.printStackTrace();
        }
    }

}
